<template>
    <el-dialog v-model="dialogvisible" title="商品选择" width="80%" destroy-on-close>
        <el-table ref="multipleTableRef" v-loading="loading" :data="tableData" stripe style="width: 100%" height="300px" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" />
            <el-table-column label="商品">
                <template #default="{ row }">
                    <div class="flex">
                        <el-image class="mr-3 rounded" :src="row.cover" fit="cover" :lazy="true" style="width: 50px; height: 50px"> </el-image>
                        <div class="flex-1">
                            <p>{{ row.title }}</p>
                            <p class="mb-1 text-xs text-gray-400">分类:{{ row.category ? row.category.name : '未分类' }}</p>
                            <p class="text-xs text-gray-400">创建时间：{{ row.create_time }}</p>
                        </div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="总库存" width="90" prop="stock" align="center" />
            <el-table-column label="价格（元）" width="150" align="center">
                <template #default="{ row }">
                    <span class="text-rose-500">￥{{ row.min_price }}</span>
                    <el-divider direction="vertical"></el-divider>
                    <span class="text-xs text-gray-500">￥{{ row.min_oprice }}</span>
                </template>
            </el-table-column>
        </el-table>
        <div class="mt-5 flex items-center justify-center">
            <el-pagination background layout="prev, pager ,next" :total="total" :current-page="currentPage" :page-size="limit" @current-change="getData" />
        </div>
        <template #footer>
            <span>
                <el-button @click="close">取消</el-button>
                <el-button type="primary" @click="submit">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script setup>
import { ref } from 'vue'
import { getGoodsList } from '~/api/goods.js'
import { useInitTable } from '~/composables/useCommon.js'
const dialogvisible = ref(false)

const { handleSelectionChange, multipleTableRef, searchForm, tableData, loading, currentPage, total, limit, getData, multiSelectionIds } = useInitTable({
    searchForm: {
        title: '',
        tab: 'all',
        category_id: null,
    },
    getList: getGoodsList,
    onGetListSuccess: (res) => {
        tableData.value = res.list
        total.value = res.totalCount
    },
})

function close() {
    dialogvisible.value = false
}

const callbackFunction = ref(null)
const open = (callback = null) => {
    callbackFunction.value = callback
    dialogvisible.value = true
}

const submit = () => {
    if (typeof callbackFunction.value === 'function') {
        callbackFunction.value(multiSelectionIds.value)
    }
    close()
}

defineExpose({
    open,
})
</script>
